package com.homwee.factory;

import java.util.ArrayList;
import java.util.List;

import com.homwee.listMenu.ListMenu;
import com.homwee.listMenu.util.MenuItemData;
import com.homwee.listMenu.util.MenuListView;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;
import com.mediatek.twoworlds.factory.common.MtkTvFApiDisplayTypes;
import com.mediatek.twoworlds.factory.common.MtkTvFApiDisplayTypes.ColorTempData;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class WhiteBalanceAjustService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	static WindowManager mWindowManager;
	LayoutInflater mInflater;
	LayoutParams wmParams_main;
	MenuListView mListView;
	public ComSettingManager mcomSettingManager;
	public FactorySetting factorySetting;
	MenuItemData ColorTempMode;
	MenuItemData mRGain;
	MenuItemData mGGain;
	MenuItemData mBGain;
	MenuItemData mRGainOffset;
	MenuItemData mGGainOffset;
	MenuItemData mBGainOffset;
	protected List<MenuItemData> mListItemData;
	private ListMenu mListMenu;

	private final static int MIN_VALUE=524;
	private final static int MAX_VALUE=1524;

	private Handler mHandler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what){
				case 1:{
					ColorTempData mColorTempData = factorySetting
							.getColorTempData(getColorTemp());
					Log.d("zcj","redGain:"+mColorTempData.redGain+" greenGain:"+mColorTempData.greenGain+" blueGain"+mColorTempData.blueGain);
					Log.d("zcj","redOffset:"+mColorTempData.redOffset+" greenGain:"+mColorTempData.greenOffset+" blueOffset"+mColorTempData.blueOffset);
					mRGain.updateValue(mColorTempData.redGain);
					mGGain.updateValue(mColorTempData.greenGain);
					mBGain.updateValue(mColorTempData.blueGain);
					mRGainOffset.updateValue(mColorTempData.redOffset);
					mGGainOffset.updateValue(mColorTempData.greenOffset);
					mBGainOffset.updateValue(mColorTempData.blueOffset);
				}

			}
		}
	};
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		mWindowManager = (WindowManager) getApplication().getSystemService(
				Context.WINDOW_SERVICE);
		mInflater = LayoutInflater.from(getApplication());
		mcomSettingManager = ComSettingManager
				.getInstance(WhiteBalanceAjustService.this);
		factorySetting = mcomSettingManager.getFactory();
		createMainView();
		mWindowManager.addView(mListMenu, wmParams_main);
	}

	private void createMainView() {
		wmParams_main = new LayoutParams();
		wmParams_main.type = LayoutParams.TYPE_PHONE;
		wmParams_main.format = PixelFormat.RGBA_8888;
		wmParams_main.gravity = Gravity.LEFT | Gravity.TOP;
		wmParams_main.x = 0;
		wmParams_main.y = 0;
		wmParams_main.width = LayoutParams.WRAP_CONTENT;
		wmParams_main.height = LayoutParams.MATCH_PARENT;
		mListItemData = new ArrayList<MenuItemData>();
		List<MenuItemData> mListItemDatas = loadItemData();
		mListItemData.get(0).mTitleName = "WB Adjust";
		mListMenu = new ListMenu(this);
		mListMenu.init(mListItemDatas, 0, 0);

		mListMenu.setVersion(this.getString(R.string.factory_software_version)
				+ factorySetting.getSWVersion());
		mListMenu.setVersionDate(this.getString(R.string.factory_comfile_time)
				+ factorySetting.getSWDate());
		mListMenu.setOnTitleOnkeyListener(new ListMenu.OnTitleOnkeyListener() {
			@Override
			public void onKey(int keyCode) {
				if(keyCode==4) {
					mWindowManager.removeView((View) mListMenu);
					stopSelf();
					Intent mIntent = new Intent();
					mIntent.setComponent(new ComponentName("com.homwee.factory",
							"com.homwee.factory.FactoryActivity"));
					mIntent.addFlags(mIntent.FLAG_ACTIVITY_NEW_TASK);
					mIntent.putExtra("PageIndex", 4);
					mIntent.putExtra("ItemIndex", 0);
					WhiteBalanceAjustService.this.startActivity(mIntent);
				}
			}
		});
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	public List<MenuItemData> loadItemData() {
		mListItemData.clear();


		String[] srcArray =factorySetting.getSourceList();

		MenuItemData WB_Src = new MenuItemData(0, "Source", 0,
				srcArray.length - 1, 1, srcArray, 1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		WB_Src.mInitValue = factorySetting.getSourceIndex();
		WB_Src.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "WB Source");
				factorySetting.setSourceIndex(value);

				factorySetting.setColorTempMode(2);// cool 1 standard 2 warm 3
				ColorTempMode.updateValue(1);
				mHandler.removeMessages(1);
				mHandler.sendEmptyMessageDelayed(1,500);
				/*ColorTempData mColorTempData = factorySetting
						.getColorTempData(getColorTemp());
				Log.d("zcj","redGain:"+mColorTempData.redGain+" greenGain:"+mColorTempData.greenGain+" blueGain"+mColorTempData.blueGain);
				Log.d("zcj","redOffset:"+mColorTempData.redOffset+" greenGain:"+mColorTempData.greenOffset+" blueOffset"+mColorTempData.blueOffset);
				mRGain.updateValue(mColorTempData.redGain);
				mGGain.updateValue(mColorTempData.greenGain);
				mBGain.updateValue(mColorTempData.blueGain);
				mRGainOffset.updateValue(mColorTempData.redOffset);
				mGGainOffset.updateValue(mColorTempData.greenOffset);
				mBGainOffset.updateValue(mColorTempData.blueOffset);*/
			}
		});
		WB_Src.setOnBackKey(backKey);
		String[] ColorTempArray = { "Cool", "Standard", "Warm" };

		ColorTempMode = new MenuItemData(0, "Color Temperature", 0, 2, 1,
				ColorTempArray, 1, MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		ColorTempMode.mInitValue = factorySetting.getColorTempMode();
		ColorTempMode.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "ColorTempMode:" + value);
				factorySetting.setColorTempMode(value+1);//cool 1 standard 2 warm 3
				ColorTempData mColorTempData = factorySetting
						.getColorTempData(getColorTemp());

				mRGain.updateValue(mColorTempData.redGain);
				mGGain.updateValue(mColorTempData.greenGain);
				mBGain.updateValue(mColorTempData.blueGain);
				mRGainOffset.updateValue(mColorTempData.redOffset);
				mGGainOffset.updateValue(mColorTempData.greenOffset);
				mBGainOffset.updateValue(mColorTempData.blueOffset);

			}
		});

		mRGain = new MenuItemData(0, "R Gain", MIN_VALUE, MAX_VALUE, 0, 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mRGain.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "mRGain"+value);
				factorySetting.setRGain(getUIColorTempValue(value));
				ColorTempData mColorTempData = new ColorTempData(value,
						mGGain.mInitValue, mBGain.mInitValue,
						mRGainOffset.mInitValue, mGGainOffset.mInitValue,
						mBGainOffset.mInitValue);
				factorySetting.setWbGain(getColorTemp(),mColorTempData);
			}
		});

		mGGain = new MenuItemData(0, "G Gain", MIN_VALUE, MAX_VALUE, 0, 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mGGain.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "mGGain");
				factorySetting.setGGain(getUIColorTempValue(value));
				ColorTempData mColorTempData = new ColorTempData(mRGain.mInitValue,
						value, mBGain.mInitValue,
						mRGainOffset.mInitValue, mGGainOffset.mInitValue,
						mBGainOffset.mInitValue);
				factorySetting.setWbGain(getColorTemp(),mColorTempData);
			}
		});

		mBGain = new MenuItemData(0, "B Gain", MIN_VALUE, MAX_VALUE, 0, 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mBGain.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "mBGain");
				factorySetting.setBGain(getUIColorTempValue(value));
				ColorTempData mColorTempData = new ColorTempData(mRGain.mInitValue,
						mGGain.mInitValue, value,
						mRGainOffset.mInitValue, mGGainOffset.mInitValue,
						mBGainOffset.mInitValue);
				factorySetting.setWbGain(getColorTemp(),mColorTempData);
			}
		});

		mRGainOffset = new MenuItemData(0, "R Gain Offset", MIN_VALUE, MAX_VALUE, 0, 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mRGainOffset.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "mRGain");
				factorySetting.setRGainOffset(getUIColorTempValue(value));
				ColorTempData mColorTempData = new ColorTempData(mRGain.mInitValue,
						mGGain.mInitValue, mBGain.mInitValue,
						value, mGGainOffset.mInitValue,
						mBGainOffset.mInitValue);
				factorySetting.setWbGain(getColorTemp(),mColorTempData);
			}
		});

		mGGainOffset = new MenuItemData(0, "G Gain Offset", MIN_VALUE, MAX_VALUE, 0, 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mGGainOffset.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "mGGain");
				factorySetting.setGGainOffset(getUIColorTempValue(value));
				ColorTempData mColorTempData = new ColorTempData(mRGain.mInitValue,
						mGGain.mInitValue, mBGain.mInitValue,
						mRGainOffset.mInitValue,value,
						mBGainOffset.mInitValue);
				factorySetting.setWbGain(getColorTemp(),mColorTempData);
			}
		});

		mBGainOffset = new MenuItemData(0, "B Gain Offset", MIN_VALUE, MAX_VALUE, 0, 1,
				MenuItemData.EN_ItemType.EN_OPTIONVIEW);
		mBGainOffset.setOnValueChange(new MenuItemData.onValueChange() {

			public void valueChange(int value) {
				// TODO Auto-generated method stub
				Log.v("tv", "mBGain");
				factorySetting.setBGainOffset(getUIColorTempValue(value));
				ColorTempData mColorTempData = new ColorTempData(mRGain.mInitValue,
						mGGain.mInitValue, mBGain.mInitValue,
						mRGainOffset.mInitValue, mGGainOffset.mInitValue,
						value);
				factorySetting.setWbGain(getColorTemp(),mColorTempData);
			}
		});

		ColorTempData mColorTempData = factorySetting.getColorTempData(getColorTemp());
		mRGain.mInitValue = mColorTempData.redGain;
		mGGain.mInitValue = mColorTempData.greenGain;
		mBGain.mInitValue = mColorTempData.blueGain;
		mRGainOffset.mInitValue = mColorTempData.redOffset;
		mGGainOffset.mInitValue = mColorTempData.greenOffset;
		mBGainOffset.mInitValue = mColorTempData.blueOffset;

		ColorTempMode.setOnBackKey(backKey);
		WB_Src.setOnBackKey(backKey);
		mRGain.setOnBackKey(backKey);
		mGGain.setOnBackKey(backKey);
		mBGain.setOnBackKey(backKey);
		mRGainOffset.setOnBackKey(backKey);
		mGGainOffset.setOnBackKey(backKey);
		mBGainOffset.setOnBackKey(backKey);

		mListItemData.add(WB_Src);
		mListItemData.add(ColorTempMode);
		mListItemData.add(mRGain);
		mListItemData.add(mGGain);
		mListItemData.add(mBGain);
		mListItemData.add(mRGainOffset);
		mListItemData.add(mGGainOffset);
		mListItemData.add(mBGainOffset);

		return mListItemData;
	}

	MenuItemData.onEnterBackKey backKey = new MenuItemData.onEnterBackKey() {

		@Override
		public void onEnterBackKey() {
			// TODO Auto-generated method stub
			mWindowManager.removeView((View) mListMenu);
			stopSelf();
			Intent mIntent = new Intent();
			mIntent.setComponent(new ComponentName("com.homwee.factory",
					"com.homwee.factory.FactoryActivity"));
			mIntent.addFlags(mIntent.FLAG_ACTIVITY_NEW_TASK);
			mIntent.putExtra("PageIndex", 4);
			mIntent.putExtra("ItemIndex", 0);
			WhiteBalanceAjustService.this.startActivity(mIntent);
		}
	};

	private int getUIColorTempValue(int value){
	    int temp=((value-MIN_VALUE)/5)-100;
	    return temp;
    }

    private MtkTvFApiDisplayTypes.EnumColorTemperature getColorTemp(){
		switch(ColorTempMode.mInitValue) {
			case 0:
				return MtkTvFApiDisplayTypes.EnumColorTemperature.E_MTK_FAPI_COLOR_TEMP_STANDARD;
			case 1:
				return MtkTvFApiDisplayTypes.EnumColorTemperature.E_MTK_FAPI_COLOR_TEMP_COOL;
			case 2:
				return MtkTvFApiDisplayTypes.EnumColorTemperature.E_MTK_FAPI_COLOR_TEMP_WARM;
			default:
				return MtkTvFApiDisplayTypes.EnumColorTemperature.E_MTK_FAPI_COLOR_TEMP_USER;
		}
	}

}
